package com.platform.service.impl;

import com.platform.dao.SurveyAnswerDao;
import com.platform.dao.SurveyDao;
import com.platform.dao.SurveyQuestionDao;
import com.platform.dao.SurveyUserDao;
import com.platform.entity.SurveyAnswersEntity;
import com.platform.entity.SurveyEntity;
import com.platform.entity.SurveyQuestionEntity;
import com.platform.params.SurveyAnswerParams;
import com.platform.params.SurveyQuestionParams;
import com.platform.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

@Service
public class SurveyServiceImpl implements SurveyService {

    @Autowired
    SurveyUserDao surveyUserDao;

    @Autowired
    SurveyQuestionDao surveyQuestionDao;

    @Autowired
    SurveyDao surveyDao;

    @Autowired
    SurveyAnswerDao surveyAnswerDao;

    /**
     * 新增调查表
     * @param title 调查表名称
     * @param surveyDesc 调查表描述
     * @param displayStatus 是否展示
     */
    public SurveyEntity saveSurvey(String title, String surveyDesc, int displayStatus) {
        if(title == null || surveyDesc == null ||  "".equals(title) || "".equals(surveyDesc) || displayStatus == 0){
            throw new RuntimeException("插入调查表异常，参数不正确！");
        }
        SurveyEntity surveyVo = new SurveyEntity();
        Long id = getSecureRandom();
        surveyVo.setId(id);
        surveyVo.setTitle(title);
        surveyVo.setSurveyDesc(surveyDesc);
        surveyVo.setDisplayStatus(displayStatus);
        surveyDao.save(surveyVo);
        return surveyVo;
    }

    /**
     * 新增问题表
     * @param questionParamsList
     * @return
     */
    public List<SurveyQuestionEntity> saveSurveyQuestion(List<SurveyQuestionParams> questionParamsList) {
        if(questionParamsList == null || questionParamsList.isEmpty()){
            throw new RuntimeException("未获取到问题表参数！！");
        }
        List<SurveyQuestionEntity> surveyQuestionVoList = new ArrayList<>();
        for (SurveyQuestionParams surveyQuestionParams : questionParamsList) {
            SurveyQuestionEntity surveyQuestionVo = new SurveyQuestionEntity();
            surveyQuestionVo.setId(getSecureRandom());
            surveyQuestionVo.setQuestion(surveyQuestionParams.getQuestion());
            surveyQuestionVo.setAnserType(surveyQuestionParams.getAnserType());
            surveyQuestionVo.setSurveyId(surveyQuestionParams.getSurveyId());
            surveyQuestionVoList.add(surveyQuestionVo);
        }
        surveyQuestionDao.saveBatch(surveyQuestionVoList);
        return surveyQuestionVoList;
    }

    /**
     * 保存调查表答案
     * @param answerParamsList
     * @return
     */
    public List<SurveyAnswersEntity> saveSurveyAnswer(List<SurveyAnswerParams> answerParamsList) {
        if(answerParamsList == null || answerParamsList.isEmpty()){
            throw new RuntimeException("未获取到问题表参数！！");
        }
        List<SurveyAnswersEntity> answersVoList = new ArrayList<>();
        for (SurveyAnswerParams surveyAnswerParams : answerParamsList) {
            SurveyAnswersEntity answersVo = new SurveyAnswersEntity();
            answersVo.setId(getSecureRandom());
            answersVo.setAnswerContent(surveyAnswerParams.getAnswerContent());
            answersVo.setQuestionId(surveyAnswerParams.getQuestionId());
            answersVo.setUserId(surveyAnswerParams.getUserId());
            answersVoList.add(answersVo);
        }
        surveyAnswerDao.saveBatch(answersVoList);
        return answersVoList;
    }

    /**
     * 获取随机ID
     * @return
     */
    private long getSecureRandom(){
        SecureRandom secureRandom = new SecureRandom();
        while (true){
            long random = secureRandom.nextLong();
            if(random > 0){
                return random;
            }
        }
    };



}
